import { defineStore } from 'pinia'
import {
  getAssignTree,
  updateRolePermissions
} from '@/service/module/main/system/role'
import filterPermissions from '@/utils/filter-permissions'

interface IState {
  menuList: any[]
  checkList: number[]
}

const useRoleStore = defineStore('role', {
  state: (): IState => ({
    menuList: [],
    checkList: []
  }),
  actions: {
    // 1.获取权限树
    async getAssignTreeAction(userId: number, roleId: number) {
      const permissionsRes = await getAssignTree(userId, roleId)
      const checkList = permissionsRes.data.checkList
      const menuList = permissionsRes.data.menuList
      this.menuList = menuList
      this.checkList = filterPermissions(checkList, menuList)
      // console.log(this.checkList.length, [...this.checkList])
    },

    // 2.分配权限
    async updateRolePermissionsAction(info: any) {
      await updateRolePermissions(info)
    }
  }
})

export default useRoleStore
